#version 440


layout(location = 0) in vec2 qt_TexCoord;

layout(location = 0) out vec4 fragColor;

layout(std140, binding = 0) uniform buf {
    mat4 qt_Matrix;
    float qt_Opacity;
} ubuf;

layout(binding = 1) uniform sampler2D qt_TextureY;
layout(binding = 2) uniform sampler2D qt_TextureU;
layout(binding = 3) uniform sampler2D qt_TextureV;

void main()
{
    vec3 yuv;
    vec3 rgb;

    yuv.x = texture(qt_TextureY, qt_TexCoord).r;
    yuv.y = texture(qt_TextureU, qt_TexCoord).r - 0.5;
    yuv.z = texture(qt_TextureV, qt_TexCoord).r - 0.5;

    // SDL2 BT709_SHADER_CONSTANTS
    // https://github.com/spurious/SDL-mirror/blob/4ddd4c445aa059bb127e101b74a8c5b59257fbe2/src/render/opengl/SDL_shaders_gl.c#L102
    const vec3 Rcoeff = vec3(1.1644, 0.000,   1.7927);
    const vec3 Gcoeff = vec3(1.1644, -0.2132, -0.5329);
    const vec3 Bcoeff = vec3(1.1644, 2.1124,  0.00);

    yuv.x = yuv.x - 0.0625;
    rgb.r = dot(yuv, Rcoeff);
    rgb.g = dot(yuv, Gcoeff);
    rgb.b = dot(yuv, Bcoeff);

    fragColor = vec4(rgb, 1);
}
